package tuner;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Stack;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import net.miginfocom.swing.MigLayout;

public class TunerFrame extends JFrame 
{
	public static int NUM_STRINGS=6;
	//private Tuner tuner;
	private JComboBox cBox;
	private JComboBox customBox;
	
	private StringPanel stringPanel;
	Tuner t;
	
	public TunerFrame() throws IOException
	{
		super ("Tuner");
		
		stopThreadWhenWindowCloses();		
		
		setBackground();
		
		setLayout (new MigLayout("align center"));		
		setAlwaysOnTop(true);
		setSize(500,500);
		setLocationByPlatform( true );
		
		createAndAddComboBoxOfPresetTunings();
		createAndAddPlayButtonForPresetTunings();
		
		createAndAddStopButton();
		
		add (new JLabel ("Play a custom pitch. 40 = E (Standard). Each increment of 1 = 1/2 step."), "wrap");
		createAndAddCustomComboBoxWithPlayButton();
		
		stringPanel=new StringPanel((String)cBox.getSelectedItem());
		add(stringPanel, "wrap");
		
		setResizable (true);
		setVisible(true);
		this.validate();
		this.repaint();
	}

	private void createAndAddStopButton() {
		JButton pause = new JButton ("Stop");
		pause.addActionListener (new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				if (t!= null && t.isAlive())
				{
					t.stopMe();
					//t.interrupt();
					
				}
				
			}
		});
		add (pause, "wrap");
	}

	private void stopThreadWhenWindowCloses() 
	{
		WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) 
            {
            	           	
                if (t!=null)
                {
                	t.stopMe();
                		
                	//t.interrupt();
                	
                }
            }
        };
        addWindowListener(exitListener);
	}

	private void setBackground() throws IOException {
		BufferedImage mahoganyImage = ImageIO.read(TunerFrame.class.getResource("/resources/images/TunerImages/mahogany.jpg"));
		TunerBackgroundPanel background = new TunerBackgroundPanel(mahoganyImage);
		background.setStyle(1);
		setContentPane (background);
	}
	
	private void createAndAddCustomComboBoxWithPlayButton() {
		String [] arr = new String [127];
		for (int x= 1; x <=127; x++)
		{
			arr [x-1] = "" + x;
		}
		customBox = new JComboBox (arr);
		customBox.setSelectedIndex(39);
		
		add (customBox);
		
		JButton playButton = new JButton ("Play Note");
		playButton.addActionListener (new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				int num = customBox.getSelectedIndex() +1;
				if(t!= null && t.isAlive())
				{
					
					t.stopMe();
					//t.interrupt();
				}
				t = new Tuner (num);
				
				
			}
		});
		add(playButton, "wrap");
	}


	private void createAndAddPlayButtonForPresetTunings() {
		JButton playTuning = new JButton ("Play");
		playTuning.addActionListener (new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				
				String str = (String) cBox.getSelectedItem();
				if (t!= null && t.isAlive())
				{
					t.stopMe();
					//t.interrupt();
					
				}
				t = new Tuner(str);
				
			}
		});
		add (playTuning);
	}

	private void createAndAddComboBoxOfPresetTunings() {
		String [] tuningOptions = {"Standard (E A D G B E)", "Tune down 1/2 Step (Eb, Ab, Db, Gb, Bb, Eb)",
				"Tune down 1 step (D G C F A D)", "Dropped D (D A D G B E)", "Dropped C (C G C F A D)",
				"Open C (C G C G C E)"};// creating combo box
		cBox = new JComboBox (tuningOptions);
		cBox.setSelectedIndex(0);
		
		cBox.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				stringPanel.setNewBase((String)cBox.getSelectedItem());
				
			}
			
		});
		
		add(cBox);
	}
}
